<template>
    <div class="container">
        <div class="table-breadcrumb">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>素材管理</el-breadcrumb-item>
                <el-breadcrumb-item>素材分类</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div style="margin: 20px;padding: 20px;background-color: #fff;">
            <div class="table-bar">
                <el-button type="primary" @click="handleAdd">新增分类</el-button>
                <el-popconfirm title="此操作将永久删除该文件, 是否继续?" @confirm="DeleteList" style="height: inherit;margin-left: 20px;">
                    <el-button type="danger" :disabled="selectedRowKeys.length > 0?false:true" slot="reference">删除</el-button>
                </el-popconfirm>
            </div>
            <div style="margin-top: 20px;">
                <Table :columns="listfields" :data-source="listData" :bordered="true" :pagination="false" :loading="loading" :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
                    <div slot="action" slot-scope="row, index">
                        <div style="display: flex;align-items: center;justify-content: space-around;margin-bottom: 6px">
                            <el-button type="primary" size="mini" @click="EditItem(index)">编辑</el-button>
                            <!-- <el-button type="danger" size="mini">删除</el-button> -->
                            <el-popconfirm title="此操作将永久删除该文章, 是否继续?" @confirm="DeleteItem(index)">
                                <el-button type="danger" size="mini" slot="reference">删除</el-button>
                            </el-popconfirm>
                        </div>
                    </div>
                    <div slot="img" slot-scope="row, index" style="display: flex;align-items: center;flex-wrap: wrap;justify-content: space-between;width: max-content;">
                        <img v-if="row" style="width:100px;height: 100px;" :src="row">
                        <!-- {{index}} -->
                    </div>
                </Table>
                <pagination :total="total" v-model="page" :pageSize.sync="pagesize" :showQuickJumper="true" @change="handlePageChange" @onShowSizeChange="onShowSizeChange"></pagination>
            </div>
        </div>

    </div>
</template>
<script>
    import { mapGetters, mapState, mapActions } from 'vuex';
    import pagination from '@/common/pagination.vue'
    import Table from 'ant-design-vue/lib/table';
    import modal from 'ant-design-vue/lib/modal';
    import 'ant-design-vue/lib/table/style/css'; // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
    import 'ant-design-vue/lib/modal/style/css'; // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
    export default {
        name: 'labelView',
        components: {
            pagination,
            Table,
            modal
        },
        data() {
            return {
                listData: [],
                total: 0, //定义初始有多少条数据
                page: 1, //默认第几页
                pagesize: 10, //一页显示多少条数据
                selectedRowKeys: [],
                listfields: [{
                        title: 'ID',
                        dataIndex: 'id',
                        align: 'center',
                        width: 200
                    },
                    {
                        title: '排序',
                        dataIndex: 'sort',
                        align: 'center',
                        width: 200,
                        defaultSortOrder: 'descend',
                        sorter: (a, b) => a.sort - b.sort,
                    },
                    {
                        title: 'LOGO',
                        dataIndex: 'cover',
                        scopedSlots: { customRender: 'img' },
                        width: 100,
                        align: 'center',
                    },
                    {
                        title: '名称',
                        dataIndex: 'type_name',
                        align: 'center',
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        scopedSlots: { customRender: 'action' },
                        align: 'center',
                        width: 150,
                    }
                ]
            };
        },
        computed: {
            ...mapState({
                // listfields: state => state.CustomerContact.fields,
            }),
        },
        watch: {},
        created() {},
        mounted() {
            this.getList()
        },
        methods: {
            ...mapActions({
                // GetFields: 'CustomerContact/GetFields',
            }),
            // 单个删除
            DeleteItem(row) {
                this.toDelete(row.id);
            },
            // 多个删除
            DeleteList() {
                var that = this
                var id = that.selectedRowKeys
                that.toDelete(id)
            },
            // 删除的方法
            toDelete(id_lst) {
                var that = this
                this.$http.post('/api/root/ArticleType/del', {
                    id: id_lst,
                }).then(rest => {
                    if (rest.msg === 'ok') {
                        this.getList();
                        that.selectedRowKeys = []
                        this.$message.success('删除成功');
                    } else {
                        this.$message.error("删除失败");
                    }
                })
            },
            handleAdd() {
                this.$router.push({ name: 'ClassificationAdd' })
            },
            // 编辑
            EditItem(row) {
                const id = row.id;
                this.$router.push({ name: 'ClassificationEdit', query: { id } });
            },
            //获取数据 
            getList(MES) {
                var that = this
                var params = {
                    type: 2,
                    keyword: that.keyword,
                    page: that.page,
                    pagesize: that.pagesize,
                }
                that.$http.post('api/root/ArticleType/index', params).then(res => {
                    if (res.code === 1) {
                        // console.log(res.data);
                        var data = res.data.types
                        if (data.length > 0) {
                            data.forEach((item, index, arr) => {
                                item.key = item.id
                            })
                        }
                        that.total = res.data.count
                        that.listData = data
                    } else {
                        that.$message.error(res.msg)
                    }
                }).catch(err => {
                    that.$message.error(err.msg ? err.msg : err.toString())
                })
            },
            handlePageChange(pageno, pagesize) { //点击第几页触发的函数
                this.page = pageno;
                this.pagesize = pagesize;
                this.getList()
            },
            onShowSizeChange(current, size) { //改变每页显示条目数的函数
                //pagesize 变化的回调
                this.page = current;
                this.pagesize = size;
                this.getList()
            },
            // 列表多选操作
            onSelectChange(selectedRowKeys) {
                // console.log('selectedRowKeys changed: ', selectedRowKeys);
                this.selectedRowKeys = selectedRowKeys;
            },
        },
    };
</script>
<style lang="less" scoped>
    .container {
        height: 100%;
        background-color: #f5f5f5;
    }

    .table-breadcrumb {
        height: 6%;
        min-height: 40px;
        background-color: #fff;
        display: flex;
        align-items: center;
        padding: 0 10px;
    }
</style>